<?php

class sfWidgetChoiceWithOther extends sfWidgetFormChoice{
    protected function configure($options = array(), $attributes = array()){ 
    	$this->addOption('specify_other_text', 'Specify');       
        parent::configure($options, $attributes); //TODO: amend
    }
    
    public function render($name, $value = null, $attributes = array(), $errors = array()){
        $choices = $this->getOption('choices');
        if(count($choices)){
            $choices['other'] = __('Other');
            $this->setOption('choices', $choices);
        }
        
        $other_input = tag('input', array('name'=>$name,'type'=>'text', 'disabled'=>'disabled','id'=>'other_'.$this->generateId($name)));
        $other_label = __($this->getOption('specify_other_text')).' : ';
        $other_input_cont = "<div style='display: none;' class='other_cont' id='other_cont_".$this->generateId($name)."'>".$other_label.$other_input.'</div>';
        
        $js = <<<OEF
        <script type="text/javascript">
          $(document).ready(function(){
              $('#{$this->generateId($name)}').change(function(){
                  if($(this).val() == 'other'){
                        $('#other_cont_{$this->generateId($name)}').show();
                        $('#other_{$this->generateId($name)}').removeAttr('disabled');
                  }else{
                        $('#other_cont_{$this->generateId($name)}').hide();
                        $('#other_{$this->generateId($name)}').attr('disabled','disabled');                  
                        $('#other_{$this->generateId($name)}').val('');                  
                  }
              });
              
             $('#{$this->generateId($name)}').trigger('change');
          });
          </script>
OEF
;
        return parent::render($name, $value, $attributes, $errors).$other_input_cont.$js;                
    }
}